import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import java.awt.Color;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JLabel;
import javax.swing.JComboBox;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JTextArea;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;


public class AssignTutor extends JFrame {

	private JPanel contentPane;
	String user;
	 public String crs ="";

	/**
	 * Launch the application.
	 */
	

	/**
	 * Create the frame.
	 */
	public AssignTutor(final String user) {
		
		this.user=user;
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBackground(Color.ORANGE);
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JLabel lblStudents = new JLabel("Students:");
		lblStudents.setBounds(23, 75, 61, 16);
		contentPane.add(lblStudents);
		
		final JComboBox comboBox = new JComboBox();
		comboBox.setBounds(96, 71, 147, 27);
		try{
			Connection connect = Connect.getConnection();
			Statement stat = connect.createStatement();
			
			ArrayList<String> tut = new ArrayList<String>();
			
			ResultSet ret = stat.executeQuery("SELECT DISTINCT Name,TUTOR_APP.Course_Title FROM TUTOR_APP,SECTION,STUDENT WHERE TUTOR_APP.Student_Username = STUDENT.Username AND TUTOR_APP.Course_Title=SECTION.Course_Title AND SECTION.Instructor_Username='"+ user +"'");
			while(ret.next()){
				System.out.println(ret.getString(1));
				tut.add(ret.getString(1));
				crs=ret.getString(2);
				System.out.println(crs);
			}
			System.out.print("a");
			String[] tutors = new String[tut.size()];
			for(int i=0;i<tut.size();i++){
				tutors[i]=tut.get(i);
				System.out.println(tut.get(i));
			}
			comboBox.setModel(new DefaultComboBoxModel(tutors));
		}catch(Exception e){
			//JOptionPane.showMessageDialog(contentPane,"Check Connection");
		}
		contentPane.add(comboBox);
		
		final JTextArea textArea = new JTextArea();
		textArea.setBounds(268, 52, 158, 133);
		contentPane.add(textArea);
		
		JButton btnAdd = new JButton("Add");
		btnAdd.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				textArea.append(comboBox.getSelectedItem().toString() + "\n");
			}
		});
		btnAdd.setBounds(143, 110, 117, 29);
		contentPane.add(btnAdd);
		
		JButton btnDone = new JButton("Done");
		btnDone.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
			List<String> list =  Arrays.asList(textArea.getText().split("\n"));
			List<String> username = new LinkedList<String>();
			String a="";
			
			
			
			for(String s:list){
				try{
					Connection conn = Connect.getConnection();
					Statement stat = conn.createStatement();
					ResultSet rea =stat.executeQuery("SELECT Username FROM STUDENT WHERE Name='"+s+"'");
					while(rea.next()){
						a=rea.getString(1);
						
						stat.executeUpdate("DELETE FROM TUTOR_APP WHERE Student_Username ='"+ a+"' AND Course_Title='"+crs + "'");
					}
				}catch(Exception ee){
					//JOptionPane.showMessageDialog(contentPane,"Check Connection");	
				}
			try{
				Connection conn = Connect.getConnection();
				Statement stat = conn.createStatement();
				
				ResultSet re =stat.executeQuery("SELECT Tutor_Identification  FROM TUTOR,STUDENT WHERE Name='"+s+"' AND Username=Student_Username");
				while(re.next()){
					
					
					
					stat.executeUpdate("INSERT INTO TUTORS_FOR VALUES ('"+ re.getInt(1)+"','"+ crs +"')");
					
					
				}
				
				
			}catch(Exception d){
				//JOptionPane.showMessageDialog(contentPane,"Check Connection");
			}
			}
			dispose();
			new FacultyServices(user);
			}
		});
		btnDone.setBounds(290, 210, 117, 29);
		contentPane.add(btnDone);
		setPreferredSize(new Dimension(600, 300));
		pack();
		setVisible(true);
		setTitle("Assign Tutor");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
	}
}
